import 'dart:async';
import 'package:dali_ng_pera/manager/dnp_cache_manager.dart';
import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/mob/dnp_mob.dart';
import 'package:dali_ng_pera/net/dnp_net_config.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:dali_ng_pera/tool/dnp_time_tool.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart' as dio;

class DnpRegisterController extends GetxController {

  late BuildContext ctx;

  bool hasClickCode = false;

  bool isChecked = true;

  TextEditingController topTextController = TextEditingController();

  TextEditingController bottomTextController = TextEditingController();

  FocusNode topNode = FocusNode();

  FocusNode bottomNode = FocusNode();

  int allDuration = 60;
  int leftDuration = 60;

  int began = 0;

  Timer? _timer;

  @override
  void onClose() {
    bottomNode.dispose();
    topTextController.dispose();
    bottomTextController.dispose();
    super.onClose();
  }

  void diRequest() async {
    String pater = topTextController.text;
    
    if (pater.isEmpty) {
      DnpPluginManager().showToast("please enter your phone number");
      return;
    }

    if (leftDuration != 60) {
      return;
    }

    began = DnpTimeTool.timestamp;

    DnpPluginManager().showLoading();

    var result = await DnpNetManager().post(
      '/haseye/di',
      data: dio.FormData.fromMap({
        "pater" : pater
      })
    );

    DnpPluginManager().hideLoading();

    if (result.success) {
      startDuration();
      FocusScope.of(ctx).requestFocus(bottomNode);
    }

    DnpPluginManager().showToast(result.kurios ?? "");
  }

  void christosRequest() async {
    String pater = topTextController.text;

    if (pater.isEmpty) {
      DnpPluginManager().showToast("please enter your phone number");
      return;
    }

    began = DnpTimeTool.timestamp;

    DnpPluginManager().showLoading();

    var result = await DnpNetManager().post(
        '/haseye/christos',
        data: dio.FormData.fromMap({
          "pater" : pater
        })
    );

    DnpPluginManager().hideLoading();

    if (result.success) {
      FocusScope.of(ctx).requestFocus(bottomNode);
      updateClickCode(true);
    }

    DnpPluginManager().showToast(result.kurios ?? "");
  }

  void iesousRequest() async {
    String ho = topTextController.text;
    String theos = bottomTextController.text;

    if (ho.isEmpty) {
      DnpPluginManager().showToast("please input phone number");
      return;
    }
    if (theos.isEmpty) {
      DnpPluginManager().showToast("please input verification code");
      return;
    }

    if (isChecked == false) {
      DnpPluginManager().showToast("please read and agree Privacy Policy");
      return;
    }

    DnpPluginManager().showLoading();

    var result = await DnpNetManager().post(
        '/haseye/iesous',
        data: dio.FormData.fromMap({
          "ho" : ho,
          "theos" : theos
        })
    );

    DnpPluginManager().hideLoading();
    
    if (result.success && result.iesous == "0") {

      Map map = result.close;
      String? knowledge = map["knowledge"];
      DnpCacheManager().saveString("ho_phone", map["ho"]);
      DnpNetConfig().saveKnowledge(knowledge);
      Navigator.pop(ctx);


      DnpMob.inexplicable("1", began, DnpTimeTool.timestamp);
      
    }else {
      DnpPluginManager().showToast(result.kurios ?? "");
    }
  }

  void startDuration() {
    _timer?.cancel();
    _timer = Timer.periodic(const Duration(seconds: 1), timerHandle);

    updateClickCode(true);
  }

  void timerHandle(Timer timer) {
    leftDuration = leftDuration - 1;
    if (leftDuration <= 0) {
      _timer?.cancel();
      hasClickCode = true;
      leftDuration = allDuration;
    }
    update(["updateDuration"]);
  }

  String bottomTitle() {

    bool timeFlag = leftDuration == allDuration;

    if (timeFlag && !hasClickCode) {
      return "Get code";
    }else {
      return (timeFlag && hasClickCode) ? "Get code" : "${leftDuration} s";
    }
  }

  updateClickCode(bool click) {
    hasClickCode = click;
    update(["updateClickCode"]);
  }

}